define(function(require, exports, module) {
	'use strict';
    
    var rk = require('rk');
    require('../../su/xsySuRefer/widget');

    $.widget('rk.xsyAccountRefer', $.rk.xsySuRefer, {
        options: {
            iconClass: 'xsy_su_refer_account_icon',
            business: 'account',
            createAct: 'createAccount'
        },
        _create: function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            me._super();
            me._reload();
        },
        _reload: function(){
            var me = this;
            var elem = me.element;
            var opt = me.options;

            var fromBelongId = opt.parentEntityId;
            var fromDataId = me._getDetailId();
            var fromItemId = opt.referItemId;

            var config = {
                iconClass: opt.iconClass,
                linkOfFinal: '/final/account.action?id={{id}}',
                linkOfMore: '/account.action?fromBelongId=' + fromBelongId + "&fromDataId=" + fromDataId + "&fromItemId=" + fromItemId,
                buttons: ['create'],
                children: [
                    {   
                        type: 'card'
                    },
                    {   
                        type: 'grid'
                    }
                ],
                isFold: true
            };

            var param = {};
            me._loadData(param, function (result){
                /*if(me.data_bound){
                    me.bindData(result.self);
                }else{
                    me._render(config, result);
                }*/
                me._render(config, result);
            });
        },
        _transfer: function(celldata){
            var me = this,
                opt = me.options;

            var end = '</div></div><div class="field clear"><div class="field_name">'+rk.i18n('CRM_CORE_NEW') + rk.i18n('CRM_CORE_IN_CHARGE') +'：</div><div class="field_content"><input type="text" class="text" field="transferUser" placeholder="'+rk.i18n('CRM_CORE_INPUT_SELECT')+rk.i18n('CRM_CORE_IN_CHARGE')+'"></div></div><div class="field clear"><div class="field_name">'+rk.i18n('ACCOUNT_TRANSFER_RELATED')+'：</div><div class="field_content"><div class="people_name transfer_customer"><ul class="transfer_business_type"></ul></div></div></div></div></div><div class="pop_up_bottom"><div class="pop_up_button"><a title="" href="javascript:;" class="green_button" act="save">'+rk.i18n('CRM_CORE_CONFIRM')+'</a></div></div></div>';

            var accountHtml = function(dialogElem,jsonData){
                var $ul =dialogElem.find('ul.transfer_business_type');
                var _func = jsonData.permission.func;
                var transferCustomerHtml = '';
                if (_func.contact && _func.contact.tr == 1) {
                    transferCustomerHtml += '<li><input transferFlag="contact" type="checkbox" checked/><label>' + SESSION.keywords.contact + '</label></li>';
                }
                if (_func.opportunity && _func.opportunity.tr == 1) {
                    transferCustomerHtml += '<li><input transferFlag="opportunity" type="checkbox" checked/><label>' + SESSION.keywords.opportunity + '</label></li>';
                }
                if (_func.contract && _func.contract.tr == 1) {
                    transferCustomerHtml += '<li><input transferFlag="contract" type="checkbox" checked/><label>' + SESSION.keywords.contract + '</label></li>';
                }
                if (_func.order && _func.order.tr == 1) {
                    transferCustomerHtml += '<li><input transferFlag="order" type="checkbox" checked/><label>' + SESSION.keywords.order + '</label></li>';
                }
                if (_func.cas && _func.cas.tr == 1) {
                    transferCustomerHtml += '<li><input transferFlag="cas" type="checkbox" checked/><label>' + SESSION.keywords.cas + '</label></li>';
                }
                $.each(jsonData.relationBelongs, function(i, o) {
                    if (_func[o.id] && _func[o.id].u == 1) {
                        transferCustomerHtml += '<li><input item="' + o.item + '" transferFlag="' + o.id + '" type="checkbox" checked/><label>' + o.name + '</label></li>';
                    }
                });
                $ul.append(transferCustomerHtml);
            };

            var saveActionParams = function(params,dialogElem){
                var transferContactFlag = "";
                var transferOpportunityFlag = "";
                var transferContractFlag = "";
                var transferOrderFlag = "";
                var transferCaseFlag = "";
                var relationCustomer = [];
                $.each(dialogElem.find('input[transferFlag]'), function(i, o) {
                    if ($(o).attr("checked") == "checked") {
                        var transferFlag = $(o).attr("transferFlag");
                        if (transferFlag == 'contact') {
                            transferContactFlag = 1;
                        } else if (transferFlag == 'opportunity') {
                            transferOpportunityFlag = 1;
                        } else if (transferFlag == 'contract') {
                            transferContractFlag = 1;
                        } else if (transferFlag == 'order') {
                            transferOrderFlag = 1;
                        } else if (transferFlag == 'cas') {
                            transferCaseFlag = 1;
                        } else {
                            relationCustomer.push({
                                relBusinessId: transferFlag,
                                itemId: $(o).attr("item")
                            });
                        }
                    }
                });
                params.transferContact = transferContactFlag;
                params.transferOpportunity = transferOpportunityFlag;
                params.transferContract = transferContractFlag;
                params.transferOrder = transferOrderFlag;
                params.transferCase = transferCaseFlag;
                params.relationCustomer = JSON.stringify(relationCustomer);
                return params;
            };

            var optionParams = {
                endHtml:end,
                accountHtml:accountHtml,
                saveActionParams:saveActionParams
            };

            me.do_transfer(celldata,function(){
                me._reload();
                me._triggerOuterEvent('referDataUpdated');
            },optionParams);
        },
        _delete: function(celldata){
            var me = this;
            var elem = me.element;
            var opt = me.options;
            

            var businessData = {};
            businessData.validateUrl = '/json/crm_account/validate-delete.action';
            businessData.url = '/json/crm_account/delete.action';
            businessData.postJson = {
                accountIds : celldata.id
            }
            businessData.confirmTip = function(json){
                var html_substr = celldata.config.title + '【' + celldata.name + '】';

                var html = rk.i18n('ACCOUNT_DELETE_ARRAY_STRING_1', html_substr);
                if (json.status == 0) {
                    if (json.child.childSum > 0) {
                        html = rk.i18n('ACCOUNT_DELETE_ARRAY_STRING_2',html_substr, json.child.childSum);
                    }else{
                        var html_substr = celldata.config.title + '【' + celldata.name + '】';
                        html = rk.i18n('ACCOUNT_DELETE_ARRAY_STRING_1', html_substr);
                    }
                }else if(json.status == 200001){
                    html = json.child.content;
                }
                return html;
            }

            me.do_cascadeDelete(celldata, businessData);

        }


    });
});
